/*
 * @Author: szx
 * @Date: 2022-04-09 15:04:08
 * @LastEditTime: 2022-04-09 15:07:20
 * @Description:
 * @FilePath: \leetcode\500-599\503\503_2.js
 */
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function (nums) {
    const n = nums.length;
    const res = new Array(n).fill(-1);
    const st = [nums[0]];
    for (let i = 1; i < n * 2; i++) {
        const num = nums[i % n];
        while (st.length && st[st.length - 1] > num) {
            const top = st.pop();
            res[top] = num;
        }
        res.push(num);
    }
    return res;
};
